/**
 * 
 */
package com.jushu.storbox.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileInfo;
import com.jushu.storbox.entities.FolderInfo;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.model.request.FolderBaseRequest;
import com.huawei.sharedrive.sdk.android.model.request.FolderCreateRequest;
import com.huawei.sharedrive.sdk.android.modelV2.request.FolderCopyRequstV2;
import com.huawei.sharedrive.sdk.android.modelV2.request.FolderListRequestV2;
import com.huawei.sharedrive.sdk.android.modelV2.request.FolderMoveRequstV2;
import com.huawei.sharedrive.sdk.android.modelV2.request.Order;
import com.huawei.sharedrive.sdk.android.modelV2.response.FolderListResponseV2;
import com.huawei.sharedrive.sdk.android.modelV2.response.FolderResponse;
import com.huawei.sharedrive.sdk.android.serviceV2.FolderClientV2;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

public class RemoteShareFolderService extends AbstractBaseService implements IRemoteShareFolderService {
	/**
	 * 
	 */
	public RemoteShareFolderService() {
	}

	@Override
	public FolderResponse getFolderResponse(String ownerId, String folderId) throws ClientException {
		FolderBaseRequest request = new FolderBaseRequest();
		request.setFolderID(folderId);
		request.setAuthorization(this.getAuthCode());
		request.setOwnerID(ownerId);
        shareDriveApplication.smartAuthorization();
		FolderResponse response = FolderClientV2.getInstance().getFolderInfo(request);
		return response;
	}

	@Override
	public FolderListResponseV2 getFolderListResponse(String folderID, String des, int limit, int offset, String orderby)
			throws ClientException {		
		FolderListRequestV2 request = new FolderListRequestV2();
		request.setFolderID(folderID);
		request.setLimit(limit);
		request.setOffset(offset);
		request.setOwnerID(ShareDriveApplication.getInstance().getWnerID());

		Order o = new Order();
		o.setDirection(des);
		o.setField(orderby);
		List<Order> olist = new ArrayList<Order>();
		olist.add(o);
		request.setOrder(olist);

        shareDriveApplication.smartAuthorization();
		FolderListResponseV2 response = FolderClientV2.getInstance()
				.getFolderInfoList(request, this.getAuthCode());
		
		return response;
	}

	public List<FileFolderInfo> getFolderList(Context context, String folderID, String des, int limit, int offset,
			String orderby) throws ClientException {
		FolderListRequestV2 request = new FolderListRequestV2();
		request.setFolderID(folderID);
		request.setLimit(limit);
		request.setOffset(offset);
		request.setOwnerID(ShareDriveApplication.getInstance().getWnerID());

		Order o = new Order();
		o.setDirection(des);
		o.setField(orderby);
		List<Order> olist = new ArrayList<Order>();
		olist.add(o);
		request.setOrder(olist);

        shareDriveApplication.smartAuthorization();
		FolderListResponseV2 response = FolderClientV2.getInstance()
				.getFolderInfoList(request, this.getAuthCode());
		
		List<FileFolderInfo> folderList = FileHelper.convertFolderRemoteToLocal(response);
		return folderList;
	}

	@Override
	public FolderResponse renameFolder(String folderID, String name) throws ClientException {
		return null;
	}

	@Override
	public FolderResponse renameFolderInfo(FolderBaseRequest request, String name) throws ClientException {
        shareDriveApplication.smartAuthorization();
		FolderResponse response = FolderClientV2.getInstance().renameFolderInfo(request, name);
		return response;
	}

	@Override
	public String deleteFolder(String ownerId, String folderID) throws ClientException {
		String response = "";
		FolderBaseRequest fbr = new FolderBaseRequest();
		fbr.setAuthorization(this.getAuthCode());
		fbr.setFolderID(folderID);
		fbr.setOwnerID(ownerId);
        shareDriveApplication.smartAuthorization();
		response = FolderClientV2.getInstance().deleteFolder(fbr);
		return response;
	}

	public String deleteFolder(FolderBaseRequest fbr) throws ClientException {
        shareDriveApplication.smartAuthorization();
		String deleteFolderRes = FolderClientV2.getInstance().deleteFolder(fbr);
		return deleteFolderRes;
	}

	@Override
	public FolderResponse createFolder(String ownerId, FolderCreateRequest request) throws ClientException {
        shareDriveApplication.smartAuthorization();
		return FolderClientV2.getInstance().create(ownerId, request, this.getAuthCode());
	}

	@Override
	public FolderResponse moveFolder(String ownerId, String folderID, FolderMoveRequstV2 request) throws ClientException {
        shareDriveApplication.smartAuthorization();
		return FolderClientV2.getInstance().moveFolder(ownerId, folderID, this.getAuthCode(), request);
	}

	@Override
	public FolderResponse copyFolder(String ownerId, String folderID, FolderCopyRequstV2 request) throws ClientException {
        shareDriveApplication.smartAuthorization();
		FolderResponse response = FolderClientV2.getInstance().copyFolder(ownerId, folderID,
				this.getAuthCode(), request);
		return response;
	}

	@Override
	public boolean isUpdate(Context context, FileFolderInfo info) throws ClientException {
		return isUpdate(context, info.getOwnerId(), info.getId());
	}
	
	private boolean isUpdate(Context context, String ownerId, String folderId) throws ClientException {
		boolean isUpdate = false;
		FolderListRequestV2 request = new FolderListRequestV2();
		request.setFolderID(folderId);
		request.setLimit(1000);
		request.setOffset(0);
		request.setOwnerID(ShareDriveApplication.getInstance().getWnerID());

		Order o = new Order();
		o.setDirection("DESC");
		o.setField("modifiedAt");
		List<Order> olist = new ArrayList<Order>();
		olist.add(o);
		request.setOrder(olist);

        shareDriveApplication.smartAuthorization();
		FolderListResponseV2 response = FolderClientV2.getInstance()
				.getFolderInfoList(request, ShareDriveApplication.getInstance().getAuthorization());
		
		if (null != response) {
			List<FolderInfo> localFolderList = ServiceFactory.instance(context).getLocalFolderService()
					.getFolderList(ownerId, folderId);
			List<FileInfo> localFileList = ServiceFactory.instance(context).getLocalFileService().getFileList(ownerId, folderId);
			boolean isFileUpdate = FileHelper.isFileUpdate(context, response.getFiles(), localFileList);

			if (isFileUpdate) {
				isUpdate = true;
				return true;
			}

			List<FolderResponse> folderResponseList = response.getFolders();
			boolean isFolderUpdate = FileHelper.isFolderUpdate(context, response.getFolders(), localFolderList);

			if (isFolderUpdate) {
				isUpdate = true;
				return isUpdate;
			}

			for (FolderResponse folderResponse : folderResponseList) {
				boolean childUpdate = isUpdate(context, folderResponse.getOwnerBy(), folderResponse.getId());
				if (childUpdate) {
					return true;
				}
			}
		}
		return isUpdate;
	}
	
	@Override
	public FolderResponse copyFolderToTarget(FileFolderInfo savedFileFolderInfo, FileFolderInfo targetFolder)
			throws ClientException {
		
		String folderID = savedFileFolderInfo.getiNodeId();
		if (StringUtil.isBlank(folderID)) {
			folderID = savedFileFolderInfo.getId();
		}
		
		FolderCopyRequstV2 request = new FolderCopyRequstV2();
		request.setDestParent(targetFolder.getId());
		request.setDestOwnerId(ShareDriveApplication.getInstance().getWnerID());
		
        shareDriveApplication.smartAuthorization();
		return FolderClientV2.getInstance().copyFolder(savedFileFolderInfo.getOwnerBy(), 
				folderID, ShareDriveApplication.getInstance().getAuthorization(), request);
	}
}
